//
//  AfanStatusViewModel.swift
//  weibo
//
//  Created by afan on 16/3/27.
//  Copyright © 2016年 阿范. All rights reserved.
//

import UIKit

class AfanStatusViewModel: NSObject {
    
    static let sharedInstance: AfanStatusViewModel = AfanStatusViewModel()
    
    
    
    ///加载 weibo 数据
    func loadStatus(loadStatusCallback: (statuses: [AfanStatus]?,error: NSError?) -> ()) {
        // url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let parameters = ["access_token": AfanUserAccountViewModel.sharedInstance.userAccount!.access_token!]
        
        AfanNetworkTool.sharedInstance.requestGET(urlString, parameters: parameters) { (result, error) -> () in
            if error != nil{
                loadStatusCallback(statuses: nil, error: error)
                return
            }
            let statusesArray = result?["statuses"] as? [[String : AnyObject]]
            var statuses = [AfanStatus]()
            
            for dict in statusesArray!{
                let status = AfanStatus(dict: dict)
                statuses.append(status)
            }
            print("xxxx\(statuses)")
            loadStatusCallback(statuses: statuses, error: nil) 
            
        }
    }
}
